﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.IO;

namespace VINETLIFE.AdminCP
{
    public partial class BusinessPartner : System.Web.UI.Page
    {
        cmsPartnerTypeDO objPart = new cmsPartnerTypeDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            Functions.ddlDatabinder(ddlTrangThai, cmsSytemStatusDO.SYSTEMSTATUS_FIELD, cmsSytemStatusDO.SYSTEMSTATUSTEXT_FIELD, new cmsSytemStatusBL().SelectByType("PARTNER"));
            if (!IsPostBack)
                bindToDropDown(cboCategory);
            if (Request.QueryString["PartnerTypeId"] != null)
            {
                objPart.PartnerTypeId = int.Parse(Request.QueryString["PartnerTypeId"].ToString());

                if (!IsPostBack)
                {
                    initForm();
                }
            }

        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsPartnerTypeBL().SelectAll(); //new DataView(new cmsMenuBL().SelectAll(),"IsContent = 0","",DataViewRowState.CurrentRows).ToTable();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["PartnerTypeId"].ToString();
                    item.Text = "- " + dr["PartnerType"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["PartnerTypeId"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "+ " + dr["PartnerType"].ToString();
                    listChild.Value = dr["PartnerTypeId"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                }
            }
        }
        private void initForm()
        {
            try
            {
                objPart = new cmsPartnerTypeBL().Select(objPart);
                txtPart.Text = objPart.PartnerType;
                txtMoTa.Text = objPart.Description;
                txtDetail.Content = objPart.Detail;
                txtOrderID.Text = objPart.OrderId.ToString();
                cboCategory.SelectedValue = objPart.ParentID.ToString();
                ddlTrangThai.SelectedValue = objPart.SystemStatusId.ToString();
            }
            catch { }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objPart.PartnerTypeId <= 0)
            {
                new cmsPartnerTypeBL().Insert(objPart);
            }
            else
            {
                new cmsPartnerTypeBL().Update(objPart);
            }
            Functions.Alert("Cập nhật thành công!", "ListBusinessPartner.aspx");
        }
        private void initObject()
        {
            objPart = new cmsPartnerTypeBL().Select(objPart);
            objPart.PartnerType = txtPart.Text;
            objPart.Description = txtMoTa.Text;
            objPart.Detail = txtDetail.Content;
            objPart.OrderId = int.Parse(txtOrderID.Text);
            objPart.ParentID =int.Parse(cboCategory.SelectedValue);
            objPart.CreatedDate = DateTime.Now;
            objPart.SystemStatusId = ddlTrangThai.SelectedValue;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/ListBusinessPartner.aspx");
        }
    }
}